package com.region.spring.client;

import com.region.http.client.core.RegionClientFactory;
import com.region.spring.RegionClientFactoryBean;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author liujieyu
 * @date 2024/7/17 16:41
 * @desciption
 */
public class RegionSpringFactoryBean<T> implements FactoryBean<T> {

    private Class<T> clazz;

    private RegionClientFactory factory;

    private RegionClientFactoryBean factoryBean;

    public RegionSpringFactoryBean() {
    }

    public RegionSpringFactoryBean(Class<T> clazz) {
        this.clazz = clazz;
    }

    public void setFactoryBean(RegionClientFactoryBean factoryBean) {
        this.factoryBean = factoryBean;
    }

    @Override
    public T getObject() throws Exception {
        if (factory == null) {
            factory = factoryBean.getFactory();
        }
        return factory.client(clazz);
    }

    @Override
    public Class<?> getObjectType() {
        return clazz;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
